package com.b511.swpu.cementbackend.service.impl;

import com.b511.swpu.cementbackend.dto.output.SysMenuTreeOutputDto;
import com.b511.swpu.cementbackend.entity.SysMenu;
import com.b511.swpu.cementbackend.entity.SysUser;
import com.b511.swpu.cementbackend.repository.SysMenuRepository;
import com.b511.swpu.cementbackend.service.SysMenuService;
import com.b511.swpu.cementbackend.util.R;
import com.b511.swpu.cementbackend.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    private final SysMenuRepository sysMenuRepository;

    private final UserUtil userUtil;

    private static final String SEPARATOR = "-";

    public SysMenuServiceImpl(SysMenuRepository sysMenuRepository, UserUtil userUtil) {
        this.sysMenuRepository = sysMenuRepository;
        this.userUtil = userUtil;
    }

    @Override
    @Transactional
    public R addMenu(SysMenu sysMenu) {
        sysMenuRepository.save(sysMenu);
        return R.ok();
    }

    @Override
    public R getSysMenuTreeOutputDto() {
        return R.ok().putData(getSysMenuTreeOutputDtoByUser(userUtil.getCurrentUser()));
    }

    @Override
    public Set<SysMenuTreeOutputDto> getSysMenuTreeOutputDtoByUser(SysUser sysUser) {
        Set<SysMenuTreeOutputDto> result = new HashSet<>();
        Map<String, SysMenuTreeOutputDto> level1Dict = initSysMenuLevel1DictDict();
        Map<String, SysMenuTreeOutputDto> level2Dict = initSysMenuLevel2DictDict();
        sysUser.getRoles().forEach(role -> {
            role.getSysMenus().forEach(menu -> {
                String routerIndex = menu.getRouterIndex();
                if (StringUtils.hasText(routerIndex)) {
                    String[] index = routerIndex.split(SEPARATOR);
                    SysMenuTreeOutputDto level3 = new SysMenuTreeOutputDto()
                            .setName(menu.getName())
                            .setIcon(menu.getIcon()).
                            setIndex(menu.getRouterPath());
                    SysMenuTreeOutputDto level1 = level1Dict.get(index[0]);
                    SysMenuTreeOutputDto level2 = level2Dict.get(index[0] + SEPARATOR + index[1]);
                    level2.getChild().add(level3);
                    level1.getChild().add(level2);
                    result.add(level1);
                }
            });
        });
        return result;
    }

    private Map<String, SysMenuTreeOutputDto> initSysMenuLevel1DictDict() {
        Map<String, SysMenuTreeOutputDto> result = new HashMap<>();
        result.put("1", new SysMenuTreeOutputDto().setIndex("data-management").setIcon("el-icon-location").setName("数据管理").setChild(new TreeSet<>()));
        return result;
    }

    private Map<String, SysMenuTreeOutputDto> initSysMenuLevel2DictDict() {
        Map<String, SysMenuTreeOutputDto> result = new HashMap<>();
        result.put("1-1", new SysMenuTreeOutputDto().setIndex("basic-data").setName("基础数据").setChild(new TreeSet<>()));
        result.put("1-2", new SysMenuTreeOutputDto().setIndex("borehole-data").setName("井眼数据").setChild(new TreeSet<>()));
        result.put("1-3", new SysMenuTreeOutputDto().setIndex("cementing-information").setName("固井信息").setChild(new TreeSet<>()));
        return result;
    }
}
